package com.pocketmenu.persistence;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.pocketmenu.business.Constants;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CheckDataSource {
	// Database fields
		  private SQLiteDatabase database;
		  private SQLiteHelper dbHelper;
		  private String[] allColumns = 
			  { SQLiteHelper.COLUMN_ID,
				SQLiteHelper.COLUMN_CHECK_TOTAL,
				SQLiteHelper.COLUMN_CHECK_SENT,			
				SQLiteHelper.COLUMN_CHECK_PAYMENT_METHOD,
				SQLiteHelper.COLUMN_CHECK_TIP,
				SQLiteHelper.COLUMN_CHECK_DATETIME,
				SQLiteHelper.COLUMN_CHECK_ID_ORDER};

		  public CheckDataSource(Context context) {
		    dbHelper = new SQLiteHelper(context);
		  }

		  public void open() throws SQLException {
		    database = dbHelper.getWritableDatabase();
		  }

		  public void close() {
		    dbHelper.close();
		  }

		  public Check create(int total, boolean sent, String paymentMethod, int tip, Date datetime, int idOrder) {
		    ContentValues values = new ContentValues();
		    values.put(SQLiteHelper.COLUMN_CHECK_TOTAL, total);
		    values.put(SQLiteHelper.COLUMN_CHECK_SENT, sent);
		    values.put(SQLiteHelper.COLUMN_CHECK_PAYMENT_METHOD, paymentMethod);
		    values.put(SQLiteHelper.COLUMN_CHECK_TIP, tip);
		    
		    SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DATETIME_FORMAT);
		    values.put(SQLiteHelper.COLUMN_CHECK_DATETIME, dateFormat.format(datetime));
		    
		    values.put(SQLiteHelper.COLUMN_CHECK_ID_ORDER, idOrder);
		    
		    long insertId = database.insert(SQLiteHelper.TABLE_CHECK, null,
		        values);
		    
		    Cursor cursor = database.query(SQLiteHelper.TABLE_CHECK,
		        allColumns, SQLiteHelper.COLUMN_ID + " = " + insertId, null,
		        null, null, null);
		    cursor.moveToFirst();
		    Check newTemp = cursorToObject(cursor);
		    cursor.close();
		    return newTemp;
		  }

		  public void delete(Check toDelete) {
		    int id = toDelete.getId();
		    System.out.println("Check deleted with id: " + id);
		    database.delete(SQLiteHelper.TABLE_CHECK, SQLiteHelper.COLUMN_ID
		        + " = " + id, null);
		  }

		  public List<Check> getAll() {
		    List<Check> listTemp = new ArrayList<Check>();

		    Cursor cursor = database.query(SQLiteHelper.TABLE_CHECK,
		        allColumns, null, null, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		      Check temp = cursorToObject(cursor);
		      listTemp.add(temp);
		      cursor.moveToNext();
		    }
		    // Make sure to close the cursor
		    cursor.close();
		    return listTemp;
		  }

		  private Check cursorToObject(Cursor cursor) {
		    Check temp = new Check();
		    
		    temp.setId(cursor.getInt(0));
		    temp.setTotal(cursor.getInt(1));
		    
		    boolean sent = cursor.getInt(2) != 0;
		    temp.setSent(sent);
		    
		    temp.setPaymentMethod(cursor.getString(3));
		    temp.setTip(cursor.getInt(4));
		    
		    SimpleDateFormat format = new SimpleDateFormat(Constants.DATETIME_FORMAT);
		    Date datetime;
			try {
				datetime = format.parse(cursor.getString(5));
				temp.setDatetime(datetime);
			} catch (ParseException e) {				
				System.out.print("Error getting date from database [CheckDataSource:cursorToObject]");
			}		   
		    		    
		    temp.setIdOrder(cursor.getInt(6));
		    return temp;
		  }
		  		 
}
